package zhoug.demo.zui.ui;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.OrientationHelper;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import zhoug.common.adapters.recycler.dividers.ListItemDecoration;
import zhoug.common.adapters.recycler2.BaseRecyclerViewAdapter;
import zhoug.common.adapters.recycler2.BaseViewHolder;
import zhoug.common.utils.RandomUtils;
import zhoug.common.utils.ToastUtils;
import zhoug.demo.zui.R;
import zhoug.logging.Logger;
import zhoug.zui.swipe.ZUISwipeMenuLayout;
import zhoug.zui.swipe.ZUISwipeRecyclerView;

public class SwipeMenuLayoutActivity extends AppCompatActivity {
    private static final String TAG = ">>>SwipeMenuLayoutActivity";
    private TextView mBtnItem;
    private TextView mBtnDelete;
    private TextView mBtnUpdate;
    private ZUISwipeMenuLayout mSwipeMenuLayout;
    private ZUISwipeRecyclerView mRecyclerView;
    private SwipeRefreshLayout mSwipeRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_swipe_menu_layout);
        findViews();
        ZUISwipeMenuLayout.setDebug(true);
        mRecyclerView.setOpenSwipe(true);
        mSwipeMenuLayout.setOnClickListener(v -> {
            Logger.d(TAG, "onCreate:item");
            toast("item");
        });
        mBtnDelete.setOnClickListener(v -> {
            Logger.d(TAG, "onCreate:delete");
            toast("delete");
        });
        mBtnUpdate.setOnClickListener(v -> {
            Logger.d(TAG, "onCreate:update");
            toast("update");
        });


        Adapter adapter = new Adapter();
        adapter.setData(new ArrayList<>());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new ListItemDecoration(this, OrientationHelper.VERTICAL));
        mRecyclerView.setAdapter(adapter);

        mSwipeRefreshLayout.setOnRefreshListener(() -> {
            List<TestData> testData = loadData();
            adapter.loadSuccess(testData);
            mSwipeRefreshLayout.setRefreshing(false);
        });

        List<TestData> testData = loadData();
        adapter.loadSuccess(testData);
    }

    private static class TestData {
        private String name;
        private boolean isDelete;
        private boolean isCancel;

        public TestData(String name, boolean isDelete, boolean isCancel) {
            this.name = name;
            this.isDelete = isDelete;
            this.isCancel = isCancel;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public boolean isDelete() {
            return isDelete;
        }

        public void setDelete(boolean delete) {
            isDelete = delete;
        }

        public boolean isCancel() {
            return isCancel;
        }

        public void setCancel(boolean cancel) {
            isCancel = cancel;
        }
    }

    private List<TestData> mTestData = new ArrayList<>();

    private List<TestData> loadData() {
        if (mTestData.size() == 0) {
            for (int i = 0; i < 20; i++) {
                mTestData.add(new TestData(i + ": " + RandomUtils.randomChinese(5), false, false));
            }
        }else{
            Iterator<TestData> iterator = mTestData.iterator();
            while (iterator.hasNext()){
                TestData next = iterator.next();
                if(next.isDelete() && next.isCancel()){
                    iterator.remove();
                }
            }

        }
        return mTestData;
//      return Collections.singletonList("我是标题");
    }

    private void findViews() {
        mBtnItem = findViewById(R.id.btn_item);
        mBtnDelete = findViewById(R.id.btn_delete);
        mBtnUpdate = findViewById(R.id.btn_update);
        mSwipeMenuLayout = findViewById(R.id.SwipeMenuLayout);
        mRecyclerView = findViewById(R.id.recyclerView);
        mSwipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
    }

    private class Adapter extends BaseRecyclerViewAdapter<TestData> implements View.OnClickListener {

        @Override
        public int getLayoutId(int viewType) {
            return R.layout.item_swip_menu_item;
        }

        @Override
        protected void onBindData(BaseViewHolder holder, TestData data, int position, int viewType) {
            if (data != null) {
                TextView item = holder.getView(R.id.btn_item);
                TextView tv_isDelete = holder.getView(R.id.tv_isDelete);
                TextView tv_isCancel = holder.getView(R.id.tv_isCancel);
                TextView delete = holder.getView(R.id.btn_delete);
                TextView update = holder.getView(R.id.btn_update);
                item.setText(data.getName());
                tv_isDelete.setText(String.format("是否删除: %s", data.isDelete() ? "是" : "否"));
                tv_isCancel.setText(String.format("是否注销: %s", data.isCancel() ? "是" : "否"));
                delete.setOnClickListener(this);
                delete.setTag(position);
                update.setOnClickListener(this);
                update.setTag(position);

                delete.setVisibility(data.isDelete() ? View.GONE : View.VISIBLE);
                update.setVisibility(data.isCancel() ? View.GONE : View.VISIBLE);

                holder.itemView.setTag(position);
                holder.itemView.setOnClickListener(this);
                ZUISwipeMenuLayout root = (ZUISwipeMenuLayout) holder.itemView;

            }
        }

        @Override
        public void onClick(View v) {
            int pos = (int) v.getTag();
            TestData itemData = getItemData(pos);
            switch (v.getId()) {
                case R.id.btn_item:
                case R.id.root:
                    Logger.d(TAG, "onCreate:item" + itemData);
                    toast("item" + itemData);
                    break;
                case R.id.btn_delete:
                    Logger.d(TAG, "onCreate:delete" + itemData);
                    toast("删除:" + itemData);
                    itemData.setDelete(true);
                    notifyItemChanged(pos);
                    break;
                case R.id.btn_update:
                    Logger.d(TAG, "onCreate:update" + itemData);
                    toast("注销:" + itemData);
                    itemData.setCancel(true);
                    notifyItemChanged(pos);
                    break;
            }
        }
    }

    private void toast(String msg) {
        ToastUtils.toastShort(msg);
    }
}